package com.loiane.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.loiane.model.Account;
import com.loiane.model.Contact;

@Repository
public class AccountDAO implements IAccountDAO {

	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Account> getAccounts() {
		return hibernateTemplate.find("from Account");
	}

	@Override
	public void deleteAccount(int id) {
		Object record = hibernateTemplate.load(Contact.class, id);
		hibernateTemplate.delete(record);
	}

	@Override
	public com.loiane.model.Account saveAccount(com.loiane.model.Account account) {
		hibernateTemplate.saveOrUpdate(account);
		return account;
	}

}
